// pages/find/card/create-card.js
// var tcity = require("./citys.js");
var util = require('../../../utils/util.js')
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        email: '',
        detail: '',
        address: '',
        id: '',
        // provinces: [],
        // province: "",
        // citys: [],
        // city: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            imgSize: (app.window.width - 20 - 30) / 4,
            id: options.id
        })

        app.http.get("member-visiting-card/card-info", {
            data: {}
        }).then(res => {
            this.setData({
                img: res.data.info.images,
            });
            if (this.data.id) {
                this.setData({
                    cid: res.data.info.memberVisitingCard.id,
                    name: res.data.info.memberVisitingCard.name,
                    phone: res.data.info.memberVisitingCard.phone,
                    company: res.data.info.memberVisitingCard.company,
                    job: res.data.info.memberVisitingCard.job,
                    email: res.data.info.memberVisitingCard.email,
                    detail: res.data.info.memberVisitingCard.detail,
                    address: res.data.info.memberVisitingCard.address,
                });
            }

        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },

    bindName: function (e) {
        this.setData({
            name: e.detail.value
        })
    },
    bindPhone: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    bindCompany: function (e) {
        this.setData({
            company: e.detail.value
        })
    },
    bindJob: function (e) {
        this.setData({
            job: e.detail.value
        })
    },
    bindEmail: function (e) {
        this.setData({
            email: e.detail.value
        })
    },
    bindDetail: function (e) {
        this.setData({
            detail: e.detail.value
        })
    },
    /**
     * 选择位置
     */
    selectMap: function () {
        wx.chooseLocation({
            success: res => {
                this.setData({ address: res.address })
            }, fail: err => {
                app.wxToast.error(err.message);
            }
        });
    },
    /**
     * 清空位置
     */
    clearMap: function () {
        this.setData({
            address: ""
        })
    },


    /**
     * 发送消息模版
     */
    sendTemplateMsg() {
        app.http.post('wx-template-msg/send-template-msg', {
            data: {
                pid: this.data.formId,
                page: '/pages/find/find-all/card',
                type: 5,
                data: {
                    "keyword1": {
                        "value": util.formatTime(new Date),
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value": '电子名片已制作完成!在微信[发现]->[小程序]->[召呼吧]->[名片]查看自己的名片,随时分享增加自己的人气吧!',
                        "color": "#173177"
                    }
                }
            }
        })
    },

    formSubmit(e) {
        this.setData({
            formId: e.detail.formId
        })
        if (!this.data.name) {
            app.wxToast.error("请输入您的姓名");
            return;
        }
        if (!this.data.phone) {
            app.wxToast.error("请输入您的手机号");
            return;
        }
        if (!this.data.phone.match(/^1[0-9]{10}$/g)) {
            app.wxToast.error("请输入11位手机号");
            return;
        }
        if (!this.data.company) {
            app.wxToast.error("请输入您的公司名称");
            return;
        }
        if (!this.data.job) {
            app.wxToast.error("请输入您的职务");
            return;
        }

        let url = 'member-visiting-card/create-card';
        let data = {
            name: this.data.name,
            phone: this.data.phone,
            company: this.data.company,
            job: this.data.job,
            email: this.data.email,
            detail: this.data.detail,
            address: this.data.address,
            image: this.data.img
        }
        if (this.data.id) {
            url = 'member-visiting-card/update-card';
            data.id = this.data.cid;
        }
        app.http.post(url, {
            data
        }).then(res => {
            if (!this.data.id) {
                this.sendTemplateMsg();
            }

            wx.navigateBack();
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },
})